//
//  AddXYCardController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/8.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "AddXYCardController.h"
#import "AddXYCardCell.h"
#import "AddXYCardModel.h"

#import "ChooseTimeController01.h"
#import "ChooseTimeController02.h"
#import "XLYBankCardMessage.h"
#import "MainWebViewController.h"

@interface AddXYCardController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *UserNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *UseriPhoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *UserCardLabel;
@property (weak, nonatomic) IBOutlet UITextField *CardNumbLabel;
@property (weak, nonatomic) IBOutlet UILabel *CardNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *CardAnQuanLabel;
@property (weak, nonatomic) IBOutlet UILabel *CardTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *OrderTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *EndTimeLabel;
@property (weak, nonatomic) IBOutlet UIView *YHBackView;
@property (weak, nonatomic) IBOutlet UITableView *YHTableView;

@property (weak, nonatomic) IBOutlet UITextField *smsTF;
@property (weak, nonatomic) IBOutlet UIButton *smsBtn;

@property (strong, nonatomic) NSString * YHTime;
@property (strong, nonatomic) NSString * OrderTime;
@property (strong, nonatomic) NSString * EndTime;
@property (strong, nonatomic) NSMutableArray * YHArr;
@property (strong, nonatomic) NSString * YHIDStr;
@property (strong, nonatomic) MBProgressHUD * hud;


@property (nonatomic ,copy) NSString * orderNo;
@property (nonatomic ,copy) NSString * smsSeq;
@end

@implementation AddXYCardController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"添加信用卡";
    [self.UserNameLabel setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    self.UseriPhoneLabel.keyboardType = UIKeyboardTypeNumberPad;
    [self.UseriPhoneLabel setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    [self.UserCardLabel setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    self.CardNumbLabel.delegate = self;
    self.CardNumbLabel.keyboardType = UIKeyboardTypeNumberPad;
    [self.CardNumbLabel setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    self.CardAnQuanLabel.keyboardType = UIKeyboardTypeNumberPad;
    [self.CardAnQuanLabel setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    [self.smsTF setValue:KRGBColor(91, 91, 91) forKeyPath:@"placeholderLabel.textColor"];
    [self.smsBtn addTarget:self action:@selector(smsBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.YHTime = @"";
    self.OrderTime = @"";
    self.EndTime = @"";
    self.YHIDStr = @"";
    
    self.YHTableView.delegate = self;
    self.YHTableView.dataSource = self;
    self.YHTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.YHTableView registerNib:[UINib nibWithNibName:@"AddXYCardCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.YHBackView.hidden = YES;
    [self UpDataOfYHData];
    
    
    if (self.card_id.length != 0) {
        [self requestCardMSG];
    }
    
}
- (void)UpDataOfYHData{
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    
    [MainObjectWay PostWithMethod:@"BankList" andAction:@"Bank" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"银行信息 = %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"][@"list"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                AddXYCardModel * model = [AddXYCardModel mj_objectWithKeyValues:dic];
                [self.YHArr addObject:model];
//                [self.YHArr addObject:dic];
            }
        }
        [self.YHTableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.YHArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AddXYCardCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[AddXYCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    AddXYCardModel * model = self.YHArr[indexPath.row];
    cell.CellYXName.text = [NSString stringWithFormat:@"%@",model.banktype_name];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddXYCardModel * model = self.YHArr[indexPath.row];
    
    self.CardNameLabel.text = [NSString stringWithFormat:@"%@",model.banktype_name];
    self.YHIDStr = [NSString stringWithFormat:@"%@",model.banktype_id];
    self.YHBackView.hidden = YES;
}
- (IBAction)ClickOnBtnToHiddenYHBackView:(id)sender {
    [self.view endEditing:YES];
    self.YHBackView.hidden = NO;
}
- (IBAction)ClickOnbtnTohidden:(id)sender {
    self.YHBackView.hidden = YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.CardNumbLabel.text.length < 8) {
        return;
    }
    
}



- (IBAction)ClickOnBtnToChooseCardEndTime:(id)sender {
    [self.view endEditing:YES];
    
    ChooseTimeController01 * chooseV01 = [[ChooseTimeController01 alloc] init];
    chooseV01.TimeBack = ^(NSString * time) {
        self.YHTime = [NSString stringWithFormat:@"%@",time];
        self.CardTimeLabel.text = self.YHTime;
    };
    chooseV01.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:chooseV01 animated:YES completion:nil];
}

- (IBAction)ClickOnBtnToChooseOrderTime:(id)sender {
    [self.view endEditing:YES];
    ChooseTimeController02 * chooseV = [[ChooseTimeController02 alloc] init];
    chooseV.TimeBack = ^(NSString *timeStr) {
        self.OrderTime = [NSString stringWithFormat:@"%@",timeStr];
        self.OrderTimeLabel.text = self.OrderTime;
    };
    chooseV.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:chooseV animated:YES completion:nil];
}

- (IBAction)ClickOnBtnToChooseEndTime:(id)sender {
    [self.view endEditing:YES];
    ChooseTimeController02 * chooseV = [[ChooseTimeController02 alloc] init];
    chooseV.TimeBack = ^(NSString *timeStr) {
        self.EndTime = [NSString stringWithFormat:@"%@",timeStr];
        self.EndTimeLabel.text = self.EndTime;
    };
    chooseV.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:chooseV animated:YES completion:nil];
}


- (IBAction)ClickOnBtnToLookAgreementOfAddNewsCard:(id)sender {
    [self.view endEditing:YES];
    [MBProgressHUD showAlertViewWithText:@"查看协议"];
}
- (IBAction)ClickOnBtnToAddNewCardNews:(id)sender {
    
    if (self.UserNameLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"用户名不能为空"];
        return;
    }
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhoneLabel.text]) {
        [MBProgressHUD showAlertViewWithText:@"请填写正确的手机号"];
        return;
    }
    
    if (self.UserCardLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"身份证不能为空"];
        return;
    }
    
    if (self.CardNumbLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"银行卡号不能为空"];
        return;
    }
    if (self.YHIDStr.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"所属银行不能为空"];
        return;
    }
    
    if (self.CardAnQuanLabel.text.length != 3) {
        [MBProgressHUD showAlertViewWithText:@"填写正确的安全码"];
        return;
    }
    
    if (self.YHTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择有效期"];
        return;
    }
    
    if (self.OrderTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择账单日期"];
        return;
    }
    
    if (self.EndTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择还款日期"];
        return;
    }
    
    if (self.smsTF.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写验证码"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUserID forKey:@"card_member"];
    [dic setObject:self.UserNameLabel.text forKey:@"card_membername"];
    [dic setObject:self.UseriPhoneLabel.text forKey:@"card_phone"];
    [dic setObject:self.UserCardLabel.text forKey:@"card_idcard"];
    [dic setObject:self.CardNumbLabel.text  forKey:@"card_num"];
    [dic setObject:self.YHIDStr forKey:@"card_bank"];
    [dic setObject:self.CardAnQuanLabel.text forKey:@"card_cvn"];
    [dic setObject:self.CardTimeLabel.text forKey:@"card_date"];
    [dic setObject:self.OrderTimeLabel.text forKey:@"card_bill"];
    [dic setObject:self.EndTimeLabel.text forKey:@"card_pay"];
    
    [dic setObject:@"" forKey:@"card_id"];
    
    [dic setObject:self.orderNo forKey:@"orderNo"];
    [dic setObject:self.smsSeq forKey:@"smsseq"];
    [dic setObject:self.smsTF.text forKey:@"authCode"];
    
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"add" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"添加银行卡返回数据 = %@ ",responseObject);
        NSLog(@"-----%@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [MBProgressHUD showAlertViewWithText:@"添加成功"];
            NSMutableDictionary * dic = responseObject[@"data"];
//            NSString * webStr = [NSString stringWithFormat:@"%@",dic[@"url"]];
//            MainWebViewController * WebC = [[MainWebViewController alloc] init];
//            WebC.WebStr = webStr;
//            [self.navigationController pushViewController:WebC animated:YES];
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
        }
        [self.hud hideAnimated:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"错误数据 = %@",error);
        [self.hud hideAnimated:YES];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (NSMutableArray *)YHArr {
    if (!_YHArr) {
        _YHArr = [NSMutableArray array];
    }
    return _YHArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)requestCardMSG{ //原先绑定卡  获取卡信息进行修改
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
//    [dic setObject:self.card_member forKey:@"card_member"];
    [dic setObject:KUserID forKey:@"card_member"];
    [dic setObject:self.card_id forKey:@"card_id"];
    
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"getCard" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"验证码返回数据 = %@ ",responseObject);
        NSLog(@"-----%@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
//            [MBProgressHUD showAlertViewWithText:@""];
            NSMutableDictionary * dic = responseObject[@"data"];
            
            self.UserNameLabel.text = dic[@"card_name"];
            self.UseriPhoneLabel.text = dic[@"card_phone"];
            self.UserCardLabel.text = dic[@"card_idcard"];
            self.CardNumbLabel.text = dic[@"card_number"];
            self.CardNameLabel.text = dic[@"card_bank"];
            self.YHIDStr = dic[@"card_bankid"];
            self.CardAnQuanLabel.text = dic[@"card_cvn"];
            self.YHTime = dic[@"card_exp"];
            self.CardTimeLabel.text = dic[@"card_exp"];
            self.OrderTime = [NSString stringWithFormat:@"%@",dic[@"card_zd"]];
            self.OrderTimeLabel.text = [NSString stringWithFormat:@"%@",dic[@"card_zd"]];
            self.EndTime = [NSString stringWithFormat:@"%@",dic[@"card_hk"]];
            self.EndTimeLabel.text = self.EndTime;
            [self.YHTableView reloadData];
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
        }
        [self.hud hideAnimated:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"错误数据 = %@",error);
        [self.hud hideAnimated:YES];
    }];
}



- (void)smsBtnClick{
    
    if (self.UserNameLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (![MainObjectWay IsMobileWithString:self.UseriPhoneLabel.text]) {
        [MBProgressHUD showAlertViewWithText:@"请填写正确的手机号"];
        return;
    }
    
    if (self.UserCardLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (self.CardNumbLabel.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    if (self.YHIDStr.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (self.CardAnQuanLabel.text.length != 3) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (self.YHTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (self.OrderTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    if (self.EndTime.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请先将本页面信息填写完整"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUserID forKey:@"card_member"];
    [dic setObject:self.UserNameLabel.text forKey:@"card_membername"];
    [dic setObject:self.UseriPhoneLabel.text forKey:@"card_phone"];
    [dic setObject:self.UserCardLabel.text forKey:@"card_idcard"];
    [dic setObject:self.CardNumbLabel.text  forKey:@"card_num"];
    [dic setObject:self.YHIDStr forKey:@"card_bank"];
    [dic setObject:self.CardAnQuanLabel.text forKey:@"card_cvn"];
    [dic setObject:self.CardTimeLabel.text forKey:@"card_date"];
    [dic setObject:self.OrderTimeLabel.text forKey:@"card_bill"];
    [dic setObject:self.EndTimeLabel.text forKey:@"card_pay"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    [MainObjectWay PostWithMethod:@"sendCardCode" andAction:@"Creditcard" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"验证码返回数据 = %@ ",responseObject);
        NSLog(@"-----%@",responseObject[@"msg"]);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [MBProgressHUD showAlertViewWithText:@"提交成功"];
            NSMutableDictionary * dic = responseObject[@"data"];
//            NSString * webStr = [NSString stringWithFormat:@"%@",dic[@"url"]];
//            MainWebViewController * WebC = [[MainWebViewController alloc] init];
//            WebC.WebStr = webStr;
//            [self.navigationController pushViewController:WebC animated:YES];
            //            [self.navigationController popViewControllerAnimated:YES];
            
            self.orderNo = dic[@"orderNo"];
            self.smsSeq = dic[@"smsSeq"];
            
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
        }
        [self.hud hideAnimated:YES];
        
    } failure:^(NSError *error) {
        NSLog(@"错误数据 = %@",error);
        [self.hud hideAnimated:YES];
    }];
}

@end
